﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace McsWeb.Common
{
    public class TimestampHelper
    {
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000 / 1000;   //除10000调整为13位      
            return t;
        }

        /// <summary>
        /// 将Unix时间戳转换为DateTime类型时间
        /// </summary>
        /// <param name="d">double 型数字</param>
        /// <returns>DateTime</returns>
        public static System.DateTime ConvertIntDateTime(double d)
        {
            System.DateTime time = System.DateTime.MinValue;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            time = startTime.AddMilliseconds(d);
            return time;
        }

        #region T DateTimeToTimestamp<T>(DateTime time, bool hasTime = true) 将时间转换为时间戳。

        /// <summary>
        /// 将时间转换为时间戳。
        /// </summary>
        /// <typeparam name="T">数字类型，只支持long与int类型</typeparam>
        /// <param name="time">时间</param>
        /// <param name="hasTime">是否包含时间</param>
        /// <returns></returns>
        public static T DateTimeToTimestamp<T>(DateTime time, bool hasTime = true)
        {
            string result = string.Empty;
            time = !hasTime ? new DateTime(time.Year, time.Month, time.Day) : time;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));

            if (typeof(T) == typeof(long))
            {
                result = ((time.Ticks - startTime.Ticks) / 10000).ToString();//除10000调整为13位
            }
            else if (typeof(T) == typeof(int))
            {
                long num = (time.Ticks - startTime.Ticks) / 10000000;
                int subnum = num.ToString().Length <= 10 ? num.ToString().Length : 10;
                result = num.ToString().Substring(0, subnum);
            }
            else
            {
                throw new FormatException("不支持" + typeof(T) + "类型的转换。");
            }

            return (T)Convert.ChangeType(result, typeof(T));
        }

        #endregion

        #region DateTime TimestampToDateTime(...) 将时间戳转换为时间。

        /// <summary>
        /// 将时间戳转换为时间。
        /// </summary>
        /// <param name="timestamp">时间戳</param>
        /// <returns></returns>
        public static DateTime TimestampToDateTime(string timestamp)
        {
            DateTime result = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan time = new TimeSpan(Convert.ToInt64(Convert.ToInt64(timestamp).ToString().PadRight(17, '0')));
            return result.Add(time);
        }

        public static DateTime ConvertStringToDateTime(string timeStamp)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp);//+ "0000"
            DateTime dt = startTime.AddSeconds(lTime);
            //TimeSpan toNow = new TimeSpan(lTime);
            return dt;// dtStart.Add(toNow);
        }
        /// <summary>
        /// 将时间戳转换为时间。
        /// </summary>
        /// <param name="timestamp">时间戳</param>
        /// <returns></returns>
        public static DateTime TimestampToDateTime(int timestamp)
        {
            return TimestampToDateTime(timestamp.ToString());
        }

        public static TimeSpan DateTimeDiff(DateTime DateTime1, DateTime DateTime2)
        {
            //double totalSeconds = 0.0;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            //totalSeconds = ts.TotalSeconds;
            return ts;
        }

        #endregion
    }
}